package com.chijun.mform.create.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chijun.mform.R;
import com.chijun.mform.create.fragment.FormAddProcessFragment;
import com.chijun.mform.create.mode.FormApproveProcess;
import com.chijun.mform.create.mode.FormField;
import com.chijun.mform.create.mode.FormProcessToFieldPermission;
import com.example.ccj.u.ToastUtils;

import java.util.List;
import java.util.Map;


/**
 * 设置审批人员权限
 * 2024年11月10日23:38:06
 */
public class InstallPermissionAdapter extends RecyclerView.Adapter<InstallPermissionAdapter.ViewHolder>{

    public static final String OPERATION_PERMISSIONS_ONLY_READ = "zhi_du";
    public static final String OPERATION_PERMISSIONS_CAN_WRITE = "ke_bian_ji";
    public static final String OPERATION_PERMISSIONS_HIDE = "yin_cang";


    List<FormField> formFieldList;

    //字段id，权限id    0：只读 1：可编辑 2：对该审批节点隐藏
    Map<Long,Integer> longFormProcessToFieldPermissionMap; //该审批节点对各个字段的权限
    //审批节点
    FormApproveProcess approveProcess;

    /**
     * 字段类型数组
     *
     * @param approveProcess 审批节点
     * @param formProcessToFieldPermissionList 该审批节点对各个字段的权限MAP
     */
    public InstallPermissionAdapter(List<FormField> formFieldList, FormApproveProcess approveProcess, Map<Long,Integer> formProcessToFieldPermissionList){
        this.longFormProcessToFieldPermissionMap = formProcessToFieldPermissionList;
        this.approveProcess = approveProcess;
        this.formFieldList = formFieldList;
    }

    @NonNull
    @Override
    public InstallPermissionAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new InstallPermissionAdapter.ViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapater_install_permission, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull InstallPermissionAdapter.ViewHolder holder, int position) {
        FormField formField = formFieldList.get(position);
        holder.fieldName.setText(formField.getName());
        //抄送人节点不允许设置字段可编辑
        if (approveProcess.type == FormAddProcessFragment.CHAO_SONG_REN_JIE_DIAN) holder.r2.setEnabled(false);

            //初始化该节点对应每个字段的权限
        if (longFormProcessToFieldPermissionMap.containsKey(formField.getId())) {
            Integer i = longFormProcessToFieldPermissionMap.get(formField.getId());
            if (i != null) {
                if (i == FormAddProcessFragment.OPERATION_PERMISSIONS_ONLY_READ) {
                    holder.r1.setChecked(true);
                } else if (i == FormAddProcessFragment.OPERATION_PERMISSIONS_CAN_WRITE) {
                    holder.r2.setChecked(true);
                } else if (i == FormAddProcessFragment.OPERATION_PERMISSIONS_HIDE){
                    holder.r3.setChecked(true);
                }
            } else {
                longFormProcessToFieldPermissionMap.remove(formField.getId());
                holder.r1.setChecked(true);
            }
        } else {
            holder.r1.setChecked(true);
        }

        //设置选项卡点击事件
        holder.radio.setOnCheckedChangeListener((group, checkedId) -> {
            RadioButton radioButton = group.findViewById(checkedId);
            String tab = radioButton.getTag().toString();

            switch (tab) {
                case OPERATION_PERMISSIONS_ONLY_READ:
                    longFormProcessToFieldPermissionMap.remove(formField.getId());
                    break;
                case OPERATION_PERMISSIONS_CAN_WRITE:
                    longFormProcessToFieldPermissionMap.put(formField.getId(),FormAddProcessFragment.OPERATION_PERMISSIONS_CAN_WRITE);
//                    savePermission(formField,FormAddProcessFragment.OPERATION_PERMISSIONS_CAN_WRITE);
                    break;
                case OPERATION_PERMISSIONS_HIDE:
                    longFormProcessToFieldPermissionMap.put(formField.getId(),FormAddProcessFragment.OPERATION_PERMISSIONS_HIDE);
//                    savePermission(formField,FormAddProcessFragment.OPERATION_PERMISSIONS_HIDE);
                    break;
                default:
                    ToastUtils.showShort("异常选项卡");
                    break;
            }
        });
    }


    @Override
    public int getItemCount() {
        return formFieldList == null ? 0 : formFieldList.size();
    }

    public static class ViewHolder extends  RecyclerView.ViewHolder{
        private final TextView fieldName;
        private final RadioGroup radio;
        private final RadioButton r1,r2,r3;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            fieldName = itemView.findViewById(R.id.adapater_install_permission_field_name);
            radio = itemView.findViewById(R.id.adapater_install_permission_radio);
            r1 = itemView.findViewById(R.id.adapater_install_permission_read_only);
            r2 = itemView.findViewById(R.id.adapater_install_permission_can_write);
            r3 = itemView.findViewById(R.id.adapater_install_permission_hide);
        }
    }
}
